<% content_for :javascript_head do %>
    <script type="text/javascript">
        $(function(){

            $("div").click(function(e){
                var id = $(this).attr("id");
                if(id == "aaa"){
                    return false;
                }else if(id == "tree"){
                    return false;
                }else{
                    $("#tree").slideUp(300);
                }
            })

            $("#site").click(function(e){
                $("#tree").slideDown(300);
            })
            $("#tree").hover(function(){},function(){
                $("#tree").slideUp(300);
            })
        })


        function beforeSubmit(form){
            var login = $("#user_sys_login").val();
            var name = $("#user_sys_name").val();
            var password = $("#user_sys_password").val();
            var father_demo=$("#father_demo").val();
            if(login == ''){
                $("#information p").html("请填写账户ID！");
                $('#shadow').fadeIn().delay(1000).fadeOut();
                return false;
            }
            else if(name == ''){
                $("#information p").html("请填写账户名称！");
                $('#shadow').fadeIn().delay(1000).fadeOut();
                return false;
            }
            else if(password == ''){
                $("#information p").html("请填写账户密码！");
                $('#shadow').fadeIn().delay(1000).fadeOut();
                return false;
            }
             else if(father_demo == ''){
                $("#information p").html("请选择所属机构！");
                $('#shadow').fadeIn().delay(1000).fadeOut();
                return false;
            }
            return true;
        };

        function beforeSubmit_edit(form){
            var name = $("#name_edit").val();
            if(name == ''){
                $("#information_edit p").html("请填写账户名称！");
                $('#shadow_edit').fadeIn().delay(1000).fadeOut();
                return false;
            }
            return true;
        };



        $(document).ready(function(){
                $("#information p").html(status);
                $("#shadow").fadeIn().delay(2000).fadeOut();
        });

        function check_user_login(){
            var login = $("#user_sys_login").val().trim();
            $("#user_sys_login").val(login);
            var flag = "N"
            var user_login = {
                login:$("#user_sys_login").val(),
                flag:flag
            };
            jQuery.ajax({
                url:'/user_sys/show',
                data:user_login,
                type:'get',
                dataType:'text',
                success:function(result){
                    if (result=='ok'){
                        $("#information p").html('您输入的账户ID已经注册，请重新输入。');
                        $("#shadow").fadeIn().delay(2000).fadeOut();
                        $("#user_sys_login").val('');
                        return false;
                    }else{
                        return true;
                    }
                }

            });
        }

       function cl(){
           $("form[action= '/user_sys'] ")[0].reset();
       }


       function edit_data(user_id,login,name){
           $("form[action= '/user_sys'] ")[1].reset();
           var flag = "E"
           var user_edit = {
               login:login,
               user_id:user_id,
               name:name,
               flag:flag
           };
           jQuery.ajax({
               url:'/user_sys/show',
               data:user_edit,
               type:'get',
               dataType:'text',
               success:function(result){
                   var strs = new Array(); //定义一数组
                   strs = result.split(","); //字符分割
                   if (strs[0]=='ok'){
                       $("#id").val(strs[1]);
                       $("#login_edit").val(strs[2]);
                       $("#name_edit").val(strs[3]);
                       return false;
                   }else{
                       return true;
                   }
               }

           });
       }


        function checkpwd(){
            var fpwd=$("#pwd").val().trim();
            $("#pwd").val(fpwd);
            var ftpwd=$("#rpwd").val().trim();
            $("#rpwd").val(ftpwd);
            if(fpwd==""||fpwd.length<6||fpwd.length>40){
                $("#information_edit p").html("请输入6-40位数字与字母.");
                $('#shadow_edit').fadeIn().delay(2000).fadeOut();
            }
            else{
                $("#information_edit p").html("密码可用.");
                $('#shadow_edit').fadeIn().delay(2000).fadeOut();
            }
        }
        function rcheckpwd(){
            var fpwd=$("#pwd").val().trim();
            $("#pwd").val(fpwd);
            var frpwd=$("#rpwd").val().trim();
            $("#rpwd").val(frpwd);            
            if(frpwd=="")
            {
                $("#information_edit p").html("请输入确认密码.");
                $('#shadow_edit').fadeIn().delay(2000).fadeOut();

            }
            else{
                if(frpwd!=fpwd)
                {
                    $("#information_edit p").html("两次密码输入不一致，请重新输入.");
                    $('#shadow_edit').fadeIn().delay(2000).fadeOut();
                }
                else
                {
                    $("#information_edit p").html("密码输入正确.");
                    $('#shadow_edit').fadeIn().delay(2000).fadeOut();
                }
            }
        }


    </script>
<% end %>
<% content_for :stylesheet_head do %>
        <style>
              .table {
                box-shadow: 0 0;
            }
            button.button,tr td button {
                padding: 2px 10px;
                margin: 0px 10px;
                background: #009bce;
                border-radius: 4px;
                border: 1px;
                color: #fff;
            }
            .bu,button.button,tr td button {
                  padding: 2px 10px;
                  margin: 0px 10px;
                  background: #009bce;
                  border-radius: 4px;
                  border: 1px;
                  color: #fff;
              }
        </style>

<% end %>

<!-- 隐藏框 -->
<div class="modal fade" id="Modal" tabindex="-1" role="dialog" aria-labelledby="ModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="ModalLabel">账户注册</h4>
      </div>

      <%= form_for :user_sys,url: user_sys_path   do |f| %>
        <div class="modal-body">
          <table class="table">
            <tbody>
              <tr>
                <td>
                  账户ID<font color="red">*</font>
                </td>
                <td>
                  <%= f.text_field :login, :class => "form-control", :placeholder=>"请输入账户",:onblur=>'check_user_login()'%>
                </td>
              </tr>
              <tr>
                <td>
                  账户名称<font color="red">*</font>
                </td>
                <td>
                  <%= f.text_field :name, :class => "form-control", :placeholder=>"请输入账户名称"%>
                </td>
              </tr>
              <tr>
                <td>
                  账户密码<font color="red">*</font>
                </td>
                <td>
                  <%= f.password_field :password, :class => "form-control" , :placeholder=>"请输入登录密码"%>
                  <%#= password_field :user_sys,:password_digest, :class => "form-control"%>
                </td>
              </tr>
               <tr>
                  <td>
                       所属机构<span style="color: red">*</span>
                  </td>
                  <td>
                      <div  id="aaa">
                        <input name="father_section_name" class="form-control" type="text" id="site" readonly  placeholder="请选择所属机构">
                        <div id="tree_new">
                              <ul id="treeDemo_new" class="ztree"></ul>
                              </ul>
                        </div>
                        <input name="father_id" type="hidden" id='father_demo' >
                      </div>
                  </td>
                </tr>
              <tr>
                <td>
                  注册时间
                </td>
                <td>
                  <%=f.text_field :created_at, :class => "form-control" ,:value=>Time.now.strftime("%Y-%m-%d %H:%M:%S"), :readonly=>"true"%>
                </td>
              </tr>
              <tr>
                <td colspan="2">
                  <div id="shadow" style="display: none;">
                    <div id="information"><p style="color: red;font-weight: bold;"></p></div>
                  </div>
                </td>
              </tr>
            </tbody>
          </table>
        </div>

      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">
                     关闭
                </button>
        <button type="submit" class="btn btn-primary"  onclick="return beforeSubmit(this);">
                     提交
                </button>
      </div>
      <% end %>
    </div>
  </div>
</div>
<!-- 隐藏框 -->

<!-- 隐藏框 -->
<div class="modal fade" id="Modal_edit" tabindex="-1" role="dialog" aria-labelledby="ModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="ModalLabel_edit">账户注册>>编辑</h4>
      </div>

      <%= form_tag('/user_sys/user_sys_add', method: :post) do %>
      <div class="modal-body">
            <table class="table">
              <tbody>
              <tr>
                <td>
                  账户ID<font color="red">*</font>
                </td>
                <%= hidden_field :user_sys, :id, :name => "id", :id=>"id"%>
                <td>
                  <%= text_field  :user_sys,:login, :class => "form-control", :placeholder=>"请输入账户",:id=>"login_edit", :readonly=>"true"%>
                </td>
              </tr>
              <tr>
                <td>
                  账户名称<font color="red">*</font>
                </td>
                <td>
                  <%= text_field  :user_sys,:name, :class => "form-control", :placeholder=>"请输入账户名称",:id=>"name_edit"%>
                </td>
              </tr>
              <tr>
                <td>
                  账户密码
                </td>
                <td>
                  <%= password_field :user_sys,:password, :class => "form-control" ,:id=>"pwd",:onBlur => "checkpwd()" %>
                </td>
              </tr>
              <tr>
                <td>
                  确认密码
                </td>
                <td>
                  <%= password_field :user_sys, :password_confirmation,:id=>"rpwd", :class => "form-control",:onBlur => "rcheckpwd()"%>
                </td>
              </tr>
              <tr>
                <td colspan="2">
                  <div id="shadow_edit" style="display: none;">
                    <div id="information_edit"><p style="color: red;font-weight: bold;"></p></div>
                  </div>
                </td>
              </tr>
              </tbody>
            </table>
          </div>

          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">
              关闭
            </button>
            <button type="submit" class="btn btn-primary"  onclick="return beforeSubmit_edit(this);">
              提交
            </button>
          </div>
      <% end %>
    </div>
  </div>
</div>
<!-- 隐藏框 -->
    <div class="listFilter" style="margin: 20px;">
      <h2>账户管理</h2>
      <div class="divider"></div>
       <%= form_for_filterrific @filterrific do |f| %>
            <table style="width: 100%;">
                  <tbody>
                    <tr>
                      <td class="FormLabel">账户ID:</td>
                      <td><%= f.text_field :with_login %></td>
                      <td class="FormLabel">账户名称:</td>
                      <td><%= f.text_field :with_name %></td>
                      <td><%= f.submit "查询", :class=>"button" %></td>
                      <td>
                        <div class="bu" data-toggle="modal" data-target="#Modal" onclick="cl()" style="min-width: 100px;text-align: center;float: right;cursor: pointer;">
                          新增
                        </div>
                      </td>
                    </tr>
                  </tbody>
            </table>
        <% end %>
      <div class="list_table" id="ltReport_list">
        <table class="list_table" id="ltReport_list" width="100%" cellspacing="0" style="text-align: center;">
          <thead>
            <tr>
              <th >序号</th>
              <th >账户ID</th>
              <th >账户名称</th>
              <th >所属机构</th>
              <th >注册时间</th>
              <th>操作</th>
            </tr>
          </thead>
          <tbody id="tbody">

          <%  if @user_syss != nil
             odd_or_even = 0
             tmp=0
             @user_syss.each do |user_sys|
               tmp = tmp+1
          -%>
                  <tr class="odd_row">
                      <td><%= h(tmp) %></td>
                      <td><%= user_sys.login %></td>
                      <td><%= user_sys.name %></td>
                      <td><%= user_sys.sectinos_name %></td>
                      <td><% if user_sys.created_at!=nil and user_sys.created_at!=''%><%= user_sys.created_at.strftime("%Y-%m-%d %H:%M:%S") %><%end%></td>
                    <td >                    
                    <a href="javascript:" data-toggle="modal" data-target="#Modal_edit" onclick="edit_data(<%= user_sys.id %>,'<%= user_sys.login %>','<%= user_sys.name %>');" style="color:red">
                        修改
                      </a>
                      <div style="float: left;"></div> <%= link_to "删除", user_sy_path(user_sys), {style:"color:red",method: :delete, data: { confirm: '确认删除账户信息?' } } %></div></td>
                 </tr>
            <% end %>
            <% end %>
          <tr>
            <td colspan="5">
               <%= will_paginate(@user_syss,:previous_label=>'上一页',:next_label=>'下一页')  %>
            </td>
         </tr>
          </tbody>
        </table>
        <div class="WhereToLinks"></div>
      </div>
    </div>
 <% content_for :javascript_fool do %>
      <script type="text/javascript">    
                  $(function(){
                      $("div").click(function(e){
                          var id = $(this).attr("id");
                          if(id == "aaa"){
                              return false;
                          }else if(id == "tree"){
                              return false;
                          }else{
                              $("#treeDemo_new").slideUp(300);
                          }
                      })

                      $("#site").click(function(e){
                          $("#treeDemo_new").slideDown(300);
                      })
                      $("#tree").hover(function(){},function(){
                          $("#treeDemo_new").slideUp(300);
                      })
                  })

                  function json_array(data){
                             var arr=[];
                             arr[0]=data;
                             return arr;
                  }
                  // tree节点点击事件  name为这个节点的值
                  var appid=-1;
                  function zTreeOnClick(event, treeId, treeNode) {
                            $("#site").val(treeNode.name);
                            appid=treeNode.appid;
                            $("#father_demo").val(appid);
                  };
                  var setting = {
                      callback: {
                          onClick: zTreeOnClick
                      }
                  };
                  var data=<%=get_tree_root_yh.to_s.html_safe %>;
                  var zNodes =json_array(data);
                  $(function(){
                      $.fn.zTree.init($("#treeDemo_new"), setting, zNodes);                      
                  })
      </script>
    <% end %>